package com.dailywork.basepro.serialize;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.SerializationUtils;

import java.io.*;

/**
 * @author think
 * @version 1.0.0
 * @date 20191119
 * <p>序列化
 * @since 2019/11/19
 */
public class SerializationSelf {

    public static void main(String[] args) throws FileNotFoundException {
        String fileName = System.getProperty("user.dir") + File.separator + "dx.txt";
        File file = new File(fileName);
        Salary salary = new Salary(100, 200);
        Person person = new Person("dx", 28, salary);
        SerializationUtils.serialize(person, new FileOutputStream(file));

        Person person1 = SerializationUtils.deserialize(new FileInputStream(file));
        System.out.println(person1.toString());
    }
}

@Setter
@Getter
@AllArgsConstructor
@ToString
class Person implements Serializable {

    private static final long serialVersionUID = -889123183477233345L;

    private String name;
    private Integer age;
    private transient Salary salary;

    private void readObject(ObjectInputStream objectInputStream)
            throws IOException, ClassNotFoundException {
        objectInputStream.defaultReadObject();
        salary = new Salary(objectInputStream.readInt(), 0);
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {

        objectOutputStream.defaultWriteObject();
        objectOutputStream.writeInt(salary.getCommonSal());
    }
}

@Getter
@Setter
@AllArgsConstructor
@ToString
class Salary implements Serializable {

    private static final long serialVersionUID = 713443320988930398L;

    private Integer commonSal;

    private Integer jiliSal;
}
